{% extends 'base.html' %}

{% block content %}
    <div class="row mb-4">
        <div class="col-md-8">
            <h1>#{{ issue.id }}: {{ issue.title }}</h1>
        </div>
        <div class="col-md-4 text-right">
            <a href="{{ url_for('main.edit_issue', issue_id=issue.id) }}" class="btn btn-primary mr-2">编辑</a>
            {% if issue.status != 'closed' %}
            <form action="{{ url_for('main.close_issue', issue_id=issue.id) }}" method="post" class="d-inline">
                <button type="submit" class="btn btn-success" onclick="return confirm('确定要关闭这个问题吗？');">关闭问题</button>
            </form>
            {% else %}
            <span class="badge badge-success">已关闭</span>
            {% endif %}
        </div>
    </div>

    <div class="row">
        <div class="col-md-8">
            <div class="card mb-4">
            <div class="card-header">
                <h5>问题描述</h5>
            </div>
            <div class="card-body">
                <p>{{ issue.description }}</p>
                
                {% if issue.files %}
                <div class="mt-4">
                    <h6>附件</h6>
                    <div class="row mt-2">
                        {% for file in issue.files %}
                        <div class="col-md-4 mb-3">
                            <div class="border rounded p-2">
                                {% if file.is_image() %}
                                    <img src="{{ url_for('uploaded_file', filename=file.filepath) }}" alt="{{ file.filename }}" class="img-fluid rounded mb-2">
                                {% elif file.is_video() %}
                                    <video controls class="w-100 mb-2">
                                        <source src="{{ url_for('uploaded_file', filename=file.filepath) }}" type="{{ file.file_type }}">
                                        您的浏览器不支持视频播放。
                                    </video>
                                {% elif file.is_audio() %}
                                    <audio controls class="w-100 mb-2">
                                        <source src="{{ url_for('uploaded_file', filename=file.filepath) }}" type="{{ file.file_type }}">
                                        您的浏览器不支持音频播放。
                                     </audio>
                                {% else %}
                                    <i class="fas fa-file-alt text-muted mb-2"></i>
                                {% endif %}
                                <div class="d-flex justify-content-between align-items-center">
                                    <a href="{{ url_for('uploaded_file', filename=file.filepath) }}" target="_blank" class="text-primary text-sm" title="下载文件">{{ file.filename }}</a>
                                    <span class="text-muted text-xs">{{ file.get_readable_size() }}</span>
                                </div>
                                <small class="text-muted text-xs d-block mt-1">上传于: {{ file.created_at.strftime('%Y-%m-%d %H:%M') }}</small>
                            </div>
                        </div>
                        {% endfor %}
                    </div>
                </div>
                {% endif %}
            </div>
        </div>

            <div class="card mb-4">
                <div class="card-header">
                    <h5>评论 ({{ comments|length }})</h5>
                </div>
                <div class="card-body">
                    {% for comment in comments %}
                    <div class="media mb-4">
                        <div class="media-body">
                            <h6 class="mt-0">{{ comment.user.username }} <small class="text-muted">{{ comment.created_at.strftime('%Y-%m-%d %H:%M') }}</small></h6>
                            <p>{{ comment.content }}</p>
                        </div>
                    </div>
                    {% endfor %}

                    <form action="{{ url_for('main.add_comment', issue_id=issue.id) }}" method="post">
                        <div class="form-group">
                            <label for="content">添加评论</label>
                            <textarea class="form-control" id="content" name="content" rows="3" required></textarea>
                        </div>
                        <button type="submit" class="btn btn-primary">提交评论</button>
                    </form>
                </div>
            </div>
        </div>

        <div class="col-md-4">
            <div class="card mb-4">
                <div class="card-header">
                    <h5>问题信息</h5>
                </div>
                <div class="card-body">
                    <dl class="row">
                        <dt class="col-sm-4">状态</dt>
                        <dd class="col-sm-8">
                            <span class="badge {% if issue.status == 'open' %}badge-danger{% else %}badge-success{% endif %}">
                                {{ issue.status }}
                            </span>
                        </dd>

                        <dt class="col-sm-4">优先级</dt>
                        <dd class="col-sm-8">
                            <span class="badge {% if issue.priority == 'urgent' %}badge-danger{% elif issue.priority == 'high' %}badge-warning{% elif issue.priority == 'medium' %}badge-info{% else %}badge-secondary{% endif %}">
                                {{ issue.priority }}
                            </span>
                        </dd>

                        <dt class="col-sm-4">负责人</dt>
                        <dd class="col-sm-8">{{ issue.assignee.username if issue.assignee else '未分配' }}</dd>

                        <dt class="col-sm-4">创建人</dt>
                        <dd class="col-sm-8">{{ creator.username if creator else '未知用户' }}</dd>

                        <dt class="col-sm-4">创建时间</dt>
                        <dd class="col-sm-8">{{ issue.created_at.strftime('%Y-%m-%d %H:%M') }}</dd>

                        <dt class="col-sm-4">更新时间</dt>
                        <dd class="col-sm-8">{{ issue.updated_at.strftime('%Y-%m-%d %H:%M') }}</dd>

                        <dt class="col-sm-4">截止日期</dt>
                        <dd class="col-sm-8">{{ issue.due_date.strftime('%Y-%m-%d') if issue.due_date else '未设置' }}</dd>

                        <dt class="col-sm-4">分类</dt>
                        <dd class="col-sm-8">{{ issue.category if issue.category else '未分类' }}</dd>

                        <dt class="col-sm-4">可见性</dt>
                        <dd class="col-sm-8">{{ issue.visibility }}</dd>
                    </dl>
                </div>
            </div>

            <div class="card mb-4">
                <div class="card-header">
                    <h5>标签</h5>
                </div>
                <div class="card-body">
                    {% for tag in issue.tags %}
                    <span class="badge badge-secondary mr-1">{{ tag.name }}</span>
                    {% endfor %}
                </div>
            </div>

            <div class="card mb-4">
                <div class="card-header">
                    <h5>里程碑</h5>
                </div>
                <div class="card-body">
                    {{ issue.milestone.name if issue.milestone else '未设置' }}
                </div>
            </div>
        </div>
    </div>

    <div class="card mb-4">
        <div class="card-header">
            <h5>历史记录</h5>
        </div>
        <div class="card-body">
            {% if history %}
            <div class="list-group">
                {% for item in history %}
                <div class="list-group-item">
                    <p class="mb-1"><strong>{{ item.field_changed }}</strong> 被 <strong>{{ (User.query.get(item.changed_by)).username }}</strong> 修改</p>
                    <p class="mb-1"><small class="text-muted">时间: {{ item.changed_at.strftime('%Y-%m-%d %H:%M') }}</small></p>
                    {% if item.old_value != 'None' or item.new_value != 'None' %}
                    <div class="row">
                        <div class="col-md-6">
                            <p class="text-danger"><small>从:</small> {{ item.old_value if item.old_value != 'None' else '未设置' }}</p>
                        </div>
                        <div class="col-md-6">
                            <p class="text-success"><small>到:</small> {{ item.new_value if item.new_value != 'None' else '未设置' }}</p>
                        </div>
                    </div>
                    {% endif %}
                </div>
                {% endfor %}
            </div>
            {% else %}
            <p class="text-muted">没有历史记录</p>
            {% endif %}
        </div>
    </div>
{% endblock %}